﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Configuration;

namespace CRM.Web.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            int timeOut = int.Parse(Helper.GetAppKeyValue("TimeOut"));

            FormsAuthentication.SetAuthCookie(LoginUser.UserName, true, FormsAuthentication.FormsCookiePath);
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                                               1, 
                                               LoginUser.UserName, 
                                               DateTime.Now, 
                                               DateTime.Now.AddMinutes(timeOut), 
                                               false, 
                                               LoginUser.UserName);
            
            FormsIdentity identity = new FormsIdentity(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
            //添加到浏览器中
            Response.Cookies.Add(cookie);
            
            string returnUrl = Request.QueryString["ReturnUrl"];
            if (!string.IsNullOrWhiteSpace(returnUrl))
                Response.Redirect(returnUrl);
        }
    }
}
